package org.jeecg.modules.car.utils;

import java.math.BigInteger;
import java.security.MessageDigest;


public class MD5Utils
{
	static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MD5Utils .class);
    /**
     * 补0加密
     * @param inStr
     * @return
     */
    public static String getMD5(String inStr)
    {
        MessageDigest md5 = null;
        try{
            md5 = MessageDigest.getInstance("MD5");
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++){
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
    
//    public static void main(String[] args) {
//    	String str = "appid=wx2d1bef574d7f658c&body=丽景新苑102号102物业费&mch_id=1249102801&nonce_str=98c6ec8da9e83ae5717ff3a8b0b21227&notify_url=http://callback.o2o.xinfu.info/payment/weixin/notify&out_trade_no=201702211711368754446&spbill_create_ip=192.168.1.120&total_fee=3740&trade_type=APP&";
//    	str += "key=ee7d802bc49b5af7db27383618058950";
//    	System.out.println(getMD5(str));
//    }


    /**
     * 不补0加密
     * @param str
     * @return
     */
    public static String getMD52(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            throw new RuntimeException("MD5加密出现错误");
        }
    }



}
